package com.init.iblog.controller;

import com.init.iblog.constants.SystemConstants;
import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.entity.Comment;
import com.init.iblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论列表控制层
 */
@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 查询文章的评论列表
     * 需要注意的是，前端发来的请求参数必须与后端Controller中的参数一致(名称一致，位置一致)
     *  这样子Controller层才能够准确的接收到所传递的参数赋值的一一对应关系，如果参数名不一致，那么后端就无法接受到参数值
     *  加上@RequestParam注解解决这个问题
     * @param blogId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/commentList")
    public ResponseResult commentList(@RequestParam("articleId") Long blogId, Integer pageNum, Integer pageSize){

        System.out.println("++++++++++++++++进入/commentList查询文章的评论列表接口++++++++++++++++");
        System.out.println("========================前端传来的参数：blogId"+blogId+"pageNum"+pageNum+"pageSize"+pageSize);
        return commentService.commentList(SystemConstants.ARTICLE_COMMENT,blogId,pageNum,pageSize);
    }

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @PostMapping
    public ResponseResult addComment(@RequestBody Comment comment){

        System.out.println("++++++++++++++++进入/添加评论接口++++++++++++++++");
        System.out.println("====================前端传来的参数"+comment.toString());
        return commentService.addComment(comment);
    }

    /**
     * 查询友链评论
     * @return
     */
    @GetMapping("/linkCommentList")
    public ResponseResult linkCommentList(Integer pageNum, Integer pageSize){
        System.out.println("++++++++++++++++进入/linkCommentList查询友链评论接口++++++++++++++++");
        System.out.println("===================前端传来的参数pageNum"+pageNum+"pageSize"+pageSize);
        return commentService.commentList(SystemConstants.LINK_COMMENT,null,pageNum,pageSize);
    }
}
